// pages/aboutUs/aboutUs.js
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    busCirList: [], //商圈
    areaIds: '', //商圈id
    areaName: '', //商圈名称
    isIndex: false //是否首页tab
  },
  /**
   * 确认选择
   */
  submitSelect: function () {
    wx.setStorageSync("areaIds", this.data.areaIds);
    wx.setStorageSync("areaName", this.data.areaName);
    wx.setStorageSync("isFlushWantBuyCache",true);
    if (this.data.isIndex) {
      wx.switchTab({
        url: '../askToBuyIssue/askToBuyIssue'
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 选择商圈
   */
  checkArea: function (e) {
    var that = this;
    var busCirList = that.data.busCirList;
    var areaIds = [];
    var areaName = [];
    for (let area of busCirList) {
      if (area.id == e.currentTarget.dataset.id) {
        if (!area.selectStatus) {
          areaIds.push(area.id);
        }
      } else if (area.selectStatus) {
        areaIds.push(area.id);
      }
    }
    if (areaIds.length > 3) {
      wx.showModal({
        title: '提示',
        content: '最多选择三个商圈发布',
        showCancel: false,
      })
    } else {
      areaIds = [];
      for (let area of busCirList) {
        if (area.id == e.currentTarget.dataset.id) {
          area.selectStatus = !area.selectStatus;
        }

        if (area.selectStatus) {
          areaIds.push(area.id);
          areaName.push(area.areaName);
        }
      }
      that.setData({
        busCirList: busCirList,
        areaIds: myUtils.concatStr(areaIds, ","),
        areaName: myUtils.concatStr(areaName, ",")
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /*商圈选择器*/
    httpsUtils.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var areaIds = [];
        areaIds = options.ids.split(",");
        var areaName = [];
        for (let area of json.data) {
          area.selectStatus = false;
          for (let obj of areaIds) {
            if (area.id == obj) {
              area.selectStatus = true;
              areaName.push(area.areaName);
            }
          }
        }

        that.setData({
          busCirList: json.data,
          areaIds: myUtils.concatStr(areaIds, ","),
          areaName: myUtils.concatStr(areaName, ","),
          isIndex: options.isIndex == "true" ? true : false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})